<?php
namespace Futures\Controller;
use Futures\Model\OptionModel;
/**
 *  自动更新执行的控制器
 */
class UpdateOptionTwoController{

    public function update_index(){
        header("Access-Control-Allow-Origin:*");
        //$option = M('FuturesOption')->where(array('status'=>1))->find();
        $option = get_stock_data2($option['code']);  //1是卖价、3是买价、5是开盘价、9是最新价
        header('Content-Type:application/jsonp; charset=utf-8');
        exit(json_encode($option));
    }
    
    public function mogo_update(){
        set_time_limit(0);
        //ignore_user_abort();
        $M= new OptionModel('order');//集合名（表名）
        //更新
        //do{
            //$option = get_stock_data($option['code']);  //1是卖价、3是买价、5是开盘价、9是最新价
            $option_datas = get_stock_data2();
            $where['sell_price'] = array('lte',0);
            $where['end_time'] = array('lt',time());
            $where['status'] = 1;
            //$list = $M->where($where)->save($o);
            $list = $M->where($where)->select();
            foreach ($list as $k => $v) {
                if($v['direction']){    //看涨
                    $p= $option_datas['last_price']-$v['buy_price'];
                }else{
                    $p = $v['buy_price']-$option_datas['last_price'];
                }
                if($p<0){
                    $data['profit'] = -$v['invest'];
                }elseif($p==0){
                    $data['profit'] = 0;
                }else{
                    $data['profit'] = $v['invest']*(1+$v['yield_rate']);
                    
                }
                $data['sell_price']=$option_datas['last_price'];
                $data['status']=2;
                $res = $M->where(array('id'=>$v['id']))->save($data);
                if($res && $p>=0){
                    $openid = M('Follow')->where(array('id'=>$v['user_id']))->getField('openid');
                    if($p>0){
                        $note = "恒指订单(".$v['id'].")盈利";
                    }elseif($p==0){
                        $note = "恒指订单(".$v['id'].")本金退回";
                        $data['profit'] = $v['invest'];
                    }
                    modify_account($openid,$data['profit'],5,$note);
                }
                //dump($res);
            }
            //usleep(700000);
        //}while(true);
    }

}